package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcCircleMsg;
import com.visizen.wisdomclass.server.model.WcCircleMsgAttach;
import com.visizen.wisdomclass.server.model.WcCircleMsgExample;
import com.visizen.wisdomclass.server.model.dto.PraiseExt;
import com.visizen.wisdomclass.server.model.dto.WcCircleMsgWithName;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface WcCircleMsgMapper {

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcCircleMsgSqlProvider.class, method = "countByExample")
    int countByExample(WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @DeleteProvider(type = WcCircleMsgSqlProvider.class, method = "deleteByExample")
    int deleteByExample(WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @Delete({"delete from wc_circle_msg",
            "where circle_msg_id = #{circle_msg_id,jdbcType=BIGINT}"})
    int deleteByPrimaryKey(Long circle_msg_id);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @InsertProvider(type = WcCircleMsgSqlProvider.class, method = "insertSelective")
    int insertSelective(WcCircleMsg record);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcCircleMsgSqlProvider.class, method = "selectByExampleWithBLOBs")
    @Results({
            @Result(column = "circle_msg_id", property = "circle_msg_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "circle_id", property = "circle_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "msg_type", property = "msg_type", jdbcType = JdbcType.INTEGER),
            @Result(column = "pubtime", property = "pubtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "msg_content", property = "msg_content", jdbcType = JdbcType.LONGVARCHAR)})
    List<WcCircleMsg> selectByExampleWithBLOBs(WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcCircleMsgSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "circle_msg_id", property = "circle_msg_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "circle_id", property = "circle_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "msg_type", property = "msg_type", jdbcType = JdbcType.INTEGER),
            @Result(column = "pubtime", property = "pubtime", jdbcType = JdbcType.TIMESTAMP)})
    List<WcCircleMsg> selectByExample(WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @Select({
            "select",
            "circle_msg_id, circle_id, user_id, equip_id, msg_type, pubtime, msg_content",
            "from wc_circle_msg",
            "where circle_msg_id = #{circle_msg_id,jdbcType=BIGINT}"})
    @Results({
            @Result(column = "circle_msg_id", property = "circle_msg_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "circle_id", property = "circle_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "msg_type", property = "msg_type", jdbcType = JdbcType.INTEGER),
            @Result(column = "pubtime", property = "pubtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "msg_content", property = "msg_content", jdbcType = JdbcType.LONGVARCHAR)})
    WcCircleMsg selectByPrimaryKey(Long circle_msg_id);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcCircleMsgSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") WcCircleMsg record,
                                 @Param("example") WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcCircleMsgSqlProvider.class, method = "updateByExampleWithBLOBs")
    int updateByExampleWithBLOBs(@Param("record") WcCircleMsg record,
                                 @Param("example") WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcCircleMsgSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") WcCircleMsg record,
                        @Param("example") WcCircleMsgExample example);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcCircleMsgSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WcCircleMsg record);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @Update({"update wc_circle_msg",
            "set circle_id = #{circle_id,jdbcType=BIGINT},",
            "user_id = #{user_id,jdbcType=BIGINT},",
            "equip_id = #{equip_id,jdbcType=BIGINT},",
            "msg_type = #{msg_type,jdbcType=INTEGER},",
            "pubtime = #{pubtime,jdbcType=TIMESTAMP},",
            "msg_content = #{msg_content,jdbcType=LONGVARCHAR}",
            "where circle_msg_id = #{circle_msg_id,jdbcType=BIGINT}"})
    int updateByPrimaryKeyWithBLOBs(WcCircleMsg record);

    /**
     * This method was generated by MyBatis Generator. This method corresponds to the database table wc_circle_msg
     *
     * @mbggenerated
     */
    @Update({"update wc_circle_msg",
            "set circle_id = #{circle_id,jdbcType=BIGINT},",
            "user_id = #{user_id,jdbcType=BIGINT},",
            "equip_id = #{equip_id,jdbcType=BIGINT},",
            "msg_type = #{msg_type,jdbcType=INTEGER},",
            "pubtime = #{pubtime,jdbcType=TIMESTAMP}",
            "where circle_msg_id = #{circle_msg_id,jdbcType=BIGINT}"})
    int updateByPrimaryKey(WcCircleMsg record);

    @Insert({
            "insert into wc_circle_msg (circle_msg_id, circle_id, ",
            "user_id, msg_type, ",
            "pubtime, msg_content)",
            "values (#{circle_msg_id,jdbcType=BIGINT}, #{circle_id,jdbcType=BIGINT}, ",
            "#{user_id,jdbcType=BIGINT}, #{msg_type,jdbcType=INTEGER}, ",
            "#{pubtime,jdbcType=TIMESTAMP}, #{msg_content,jdbcType=LONGVARCHAR})"})
    @Options(useGeneratedKeys = true, keyColumn = "circle_msg_id", keyProperty = "circle_msg_id")
    int insert(WcCircleMsg record);

    /**
     * 根据班级ID获取当前用户的未读圈子信息数目，根据传入的时间戳做时间between
     *
     * @param classId   班级ID�?
     * @param timestamp Date类型的时间戳，�?�常是上次调用该API时保存下的时间戳,0为当前最新时�?
     */
    @Select({"select count(circle_msg_id)",
            "from wc_circle_msg cm, wc_organization_user ou",
            "where cm.user_id = ou.user_id",
            "and ou.organization_id = #{classId,jdbcType=BIGINT}",
            "and cm.pubtime >= #{timestamp,jdbcType=TIMESTAMP}"})
    int getUnreadCircleCount(@Param("classId") Long classId, @Param("timestamp") Date timestamp);

    /**
     * 根据circle_msg_id拿到对应的附件们
     */
    @Select({"select * from wc_circle_msg_attach cma",
            "where cma.circle_msg_id = #{circle_msg_id}"})
    List<WcCircleMsgAttach> getAttachesByMsgId(@Param("circle_msg_id") Long circle_msg_id);

    /**
     * 根据circle_msg_id拿到对应的赞�?
     */
    @Select({"select cmp.*, u.attach_id from wc_circle_msg_praise cmp, wc_user u",
            "where cmp.circle_msg_id = #{circle_msg_id}",
            "and u.user_id = cmp.user_id"
    })
    @Results({
            @Result(property = "attach_id", column = "attach_id")
    })
    List<PraiseExt> getPraisesByMsgId(@Param("circle_msg_id") Long circle_msg_id);

    /**
     * 获取circleMsgId对应的消�?
     */
    @Select({"SELECT cm.*, u.fullname, u.attach_id,COUNT(mc.`comment_id`) AS commentCount ",
            "FROM wc_circle_msg cm, wc_user u, wc_circle_msg_comment mc ",
            "WHERE cm.user_id = u.user_id AND mc.`circle_msg_id` = cm.`circle_msg_id`",
            "AND cm.circle_msg_id = #{circleMsgId}"
    })
    @Results({
            @Result(property = "circle_msg_id", column = "circle_msg_id"),
            @Result(property = "circle_id", column = "circle_id"),
            @Result(property = "user_id", column = "user_id"),
            @Result(property = "msg_type", column = "msg_type"),
            @Result(property = "msg_content", column = "msg_content"),
            @Result(property = "pubtime", column = "pubtime"),
            @Result(property = "fullname", column = "fullname"),
            @Result(property = "attachList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getAttachesByMsgId")),
            @Result(property = "praiseList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getPraisesByMsgId"))
    })
    List<WcCircleMsgWithName> getCircleMessageByMsgId(@Param("circleMsgId") Long circleMsgId);

    @Select({"select cm.*, u.fullname, u.attach_id from wc_circle_msg cm, wc_user u",
            "where cm.user_id = #{userId}",
            "and cm.user_id = u.user_id",
            "order by cm.pubtime desc ",
            "limit #{start}, #{count}"})
    @Results({
            @Result(property = "circle_msg_id", column = "circle_msg_id"),
            @Result(property = "circle_id", column = "circle_id"),
            @Result(property = "user_id", column = "user_id"),
            @Result(property = "msg_type", column = "msg_type"),
            @Result(property = "msg_content", column = "msg_content"),
            @Result(property = "pubtime", column = "pubtime"),
            @Result(property = "fullname", column = "fullname"),
            @Result(property = "attachList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getAttachesByMsgId")),
            @Result(property = "praiseList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getPraisesByMsgId"))
    })
    List<WcCircleMsgWithName> getPagedUserCircleMsg(@Param("userId") Long userId,
                                                    @Param("start") int start,
                                                    @Param("count") int count);

    /**
     * 根据班级ID获取当前用户圈子信息
     */
    @Select({
            "select cm.*, u.fullname, u.attach_id from wc_circle_msg cm, wc_organization_user ou, wc_user u",
            "where cm.user_id = ou.user_id",
            "and ou.user_id = u.user_id",
            "and ou.organization_id = #{classId,jdbcType=BIGINT}",
            "and cm.pubtime >= #{timestamp,jdbcType=TIMESTAMP}",
            "AND cm.circle_id = (SELECT circle_id FROM wc_circle WHERE organization_id = #{classId,jdbcType=BIGINT})",
            "group by cm.circle_msg_id",
            "order by pubtime desc",
            "limit #{count,jdbcType=INTEGER}"
    })
    @Results({
            @Result(property = "circle_msg_id", column = "circle_msg_id"),
            @Result(property = "circle_id", column = "circle_id"),
            @Result(property = "user_id", column = "user_id"),
            @Result(property = "msg_type", column = "msg_type"),
            @Result(property = "msg_content", column = "msg_content"),
            @Result(property = "pubtime", column = "pubtime"),
            @Result(property = "attach_id", column = "attach_id"),
            @Result(property = "fullname", column = "fullname"),
            @Result(property = "commentCount", column = "circle_msg_id", one = @One(select = "selectCommentCount")),
            @Result(property = "attachList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getAttachesByMsgId")),
            @Result(property = "praiseList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getPraisesByMsgId"))
    })
    List<WcCircleMsgWithName> getPagedCircleMessagesAfterTime(@Param("classId") Long classId,
                                                              @Param("timestamp") Date timestamp,
                                                              @Param("count") Integer count);

    @Select({
            "select cm.*, u.fullname, u.attach_id from wc_circle_msg cm, wc_organization_user ou, wc_user u",
            "where cm.user_id = ou.user_id",
            "and ou.user_id = u.user_id",
            "and ou.organization_id = #{classId,jdbcType=BIGINT}",
            "and cm.pubtime < #{timestamp,jdbcType=TIMESTAMP}",
            "group by cm.circle_msg_id",
            "order by pubtime desc",
            "limit #{count,jdbcType=INTEGER}"
    })
    @Results({
            @Result(property = "circle_msg_id", column = "circle_msg_id"),
            @Result(property = "circle_id", column = "circle_id"),
            @Result(property = "user_id", column = "user_id"),
            @Result(property = "msg_type", column = "msg_type"),
            @Result(property = "msg_content", column = "msg_content"),
            @Result(property = "pubtime", column = "pubtime"),
            @Result(property = "attach_id", column = "attach_id"),
            @Result(property = "fullname", column = "fullname"),
            @Result(property = "commentCount", column = "circle_msg_id", one = @One(select = "selectCommentCount")),
            @Result(property = "attachList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getAttachesByMsgId")),
            @Result(property = "praiseList", javaType = List.class, column = "circle_msg_id", many = @Many(select = "getPraisesByMsgId"))
    })
    List<WcCircleMsgWithName> getPagedCircleMessagesBeforeTime(@Param("classId") Long classId,
                                                               @Param("timestamp") Date timestamp,
                                                               @Param("count") Integer count);

    @Select("select count(*) from wc_circle_msg_comment where circle_msg_id=#{circleMsgId}")
    int selectCommentCount(@Param("circleMsgId") Long circleMsgId);
}